package com.luxsoft.em2.converters;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.SQLException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.hibernate.HibernateException;
import org.hibernate.proxy.HibernateProxy;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.luxsoft.siipap.cxc.model.Abono;
import com.luxsoft.siipap.cxc.model.Cargo;
import com.luxsoft.siipap.model.core.Cliente;
import com.luxsoft.siipap.model.core.Linea;
import com.luxsoft.siipap.model.core.Producto;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.ventas.model.Venta;
import com.luxsoft.siipap.ventas.model.VentaDet;
import com.luxsoft.sw3.ventas.Pedido;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentMapConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedMapConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedSetConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernateProxyConverter;
import com.thoughtworks.xstream.hibernate.mapper.HibernateMapper;

import com.thoughtworks.xstream.mapper.MapperWrapper;

public class XStreamConverter implements MessageConverter{
	
	XStream xstream;
	
	public XStreamConverter() {
		init();
	}
	
	public void init(){
		xstream = new XStream() {
			  protected MapperWrapper wrapMapper(final MapperWrapper next) {
			    return new HibernateMapper(next);
			  }
			};		
			xstream.registerConverter(new HibernateProxyConverter());
			xstream.registerConverter(new HibernatePersistentCollectionConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentMapConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentSortedMapConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentSortedSetConverter(xstream.getMapper()));
		//XStream xstream=new XStream();
		xstream.alias("pedido", Pedido.class);
	}

	public Message toMessage(Object object, Session session)
			throws JMSException, MessageConversionException {
		String xml=xstream.toXML(object);
		
		return session.createTextMessage(xml);
		
	}

	public Object fromMessage(Message message) throws JMSException,
			MessageConversionException {
		TextMessage tm=(TextMessage)message;
		return xstream.fromXML(tm.getText());
	}
	
	public static void main(String[] args) {
		final XStream xstream = new XStream() {
			  protected MapperWrapper wrapMapper(final MapperWrapper next) {
			    return new HibernateMapper(next);
			  }
			};		
			xstream.registerConverter(new HibernateProxyConverter());
			xstream.registerConverter(new HibernatePersistentCollectionConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentMapConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentSortedMapConverter(xstream.getMapper()));
			xstream.registerConverter(new HibernatePersistentSortedSetConverter(xstream.getMapper()));
		//XStream xstream=new XStream();
		xstream.alias("venta", Venta.class);
		/*xstream.omitField(Cliente.class, "contactos");
		xstream.omitField(Cliente.class, "comentarios");
		xstream.omitField(Cliente.class, "cuentas");
		xstream.omitField(Cargo.class, "aplicaciones");
		xstream.omitField(VentaDet.class, "producto");
		xstream.omitField(Linea.class, "children");*/
		//ServiceLocator2
		Venta v=ServiceLocator2.getVentasManager().buscarVentaInicializada("8a8a8789-35d4ff21-0135-d55c5f8a-000b");
		
		
		try {
			//stream.toXML(v,new FileOutputStream("C://t1.xml"));
			String xml=xstream.toXML(v);
			System.out.println(xml);
			Venta res=(Venta)xstream.fromXML(xml);
			System.out.println(res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*
		try {
			Venta v=(Venta)stream.fromXML(new File("C://t1.xml"));
			System.out.println(v);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
	}

}
